(function () {

  'use strict';

  angular
  .module('app')
  .controller('ExpressSelectController', ExpressSelectController);

  ExpressSelectController.$inject = ['$scope', '$http', '$window', '$rootScope', '$state', '$stateParams', 'API', 'ENUM', 'ExpressSelectService'];

  function ExpressSelectController($scope, $http, $window, $rootScope, $state,$stateParams, API, ENUM, ExpressSelectService) {

    $scope.selectedId = ExpressSelectService.expressId || null;
    $scope.goodsIds = ExpressSelectService.goodsIds || [];
    $scope.goodsNumbers = ExpressSelectService.goodsNumbers || [];
    $scope.address_id = ExpressSelectService.address_id || null;
    $scope.type = ExpressSelectService.type || '';

    $scope.vendors = [];
    $scope.touchVendor = function ( vendor ) {
        $rootScope.$emit('expressChanged', vendor);
        $scope.goBack();
    };

    function _reload() {
        var goodsIds = $scope.goodsIds;
        var goodsNumbers = $scope.goodsNumbers;
        var goods = [];

        for ( var i = 0; i < goodsIds.length && i < goodsNumbers.length; ++i ) {
            goods.push({
                goods_id: goodsIds[i],
                num:goodsNumbers[i]
            })
        }

      API.shipping.getAllExpress({
          goods: JSON.stringify(goods),
          address_id: $scope.address_id,
          type: $scope.type
      }).then(function(res) {
          $scope.vendors = res;
      });
    }

    _reload();
  }


})();
